/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.crud;

import config.ApplicationConfig;
import controller.AbstractCommand;
import java.util.Map;
import model.persistence.TransactionManager;

/**
 *
 * @author JUSCELINO
 */
public class InsertCommand extends AbstractCommand {

    public InsertCommand() {
        super("INSERT_COMMAND");

    }

    @Override
    public Object execute(Object parameter) throws Exception {
        //sempre vai passa um Map , que vai conter o DTO e a entity(nome da tabela)
        Map aux = (Map) parameter;
        String entity = (String) aux.get("entity");
        Object dto = aux.get("dto");
        ApplicationConfig appConfig = ApplicationConfig.getInstance();
        //peguei o cara que me forncece o manager
        TransactionManager manager = appConfig.getTransactionManager();
        //pronto par conexão
        try {
            manager.connect();
            dto = manager.insert(entity, dto);
            manager.commit();
        } catch (Exception erro) {
            manager.rollback();
            throw erro;
        } finally {
            manager.disconnect();
        }
        return dto;
    }
}
